package JZOffer;

public class _14 {
    class Solution {
        public ListNode ReverseList(ListNode head) {
            if (head == null) return null;
            ListNode res = head;
            ListNode pre = null;
            ListNode next = head;

            while (head != null) {
                next = head.next;
                head.next = pre;
                pre = head;
                head = next;
            }
            return pre;
        }
    }

    Solution getSolution() {
        return new Solution();
    }

    public static void main(String args[]) {
        _14 c = new _14();
        _14.Solution s = c.getSolution();
        ListNode L1 = new ListNode(3);
        ListNode L2 = new ListNode(4);
        ListNode L3 = new ListNode(5);
        ListNode L4 = new ListNode(6);
        ListNode L5 = new ListNode(7);
        L1.next = L2;
        L2.next = L3;
        L3.next = L4;
        L4.next = L5;
        ListNode res = s.ReverseList(L1);
        while (res != null) {
            System.out.println(res.val);
            res = res.next;
        }
    }
}
